package com.yikome.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yikome.utils.BoheUtils;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

	@Autowired
    private BoheUtils boheUtils;
	
	@PostMapping("/image")
    public List<String> uploadImage(HttpServletRequest request) {
		
		List<String> result = new ArrayList<>();
		
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> images = multiRequest.getFiles("images");
        for (MultipartFile image : images) {
            try {
            	String saveImage = boheUtils.saveImage(image.getOriginalFilename(), image.getInputStream());
            	result.add(saveImage);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
		return result;
    }
}
